<template>
  <div>
    <!-- 分页组件 -->
    <el-pagination
      @current-change="currentChange"
      @size-change="sizeChange"
      :page-size="pageSizes"
      layout="prev, pager,next,total,sizes,jumper"
      :total="total"
      :page-sizes="[2, 5, 10, 15, 20]"
    >
    </el-pagination>

    <!-- el-dable-column是每列的标签
    label 是当列的表头
    width当前列宽度
    prop当前列表应该显示数据字段 -->
    <el-table :data="postList">
      <el-table-column label="ID编号" prop="id" width="100"></el-table-column>

      <el-table-column label="标题" prop="title"></el-table-column>

      <el-table-column
        label="作者"
        prop="user.nickname"
        width="100"
      ></el-table-column>

      <el-table-column label="缩略图" width="200">
        <template slot-scope="scope">
          <img
            class="thumbnail"
            v-if="scope.row.cover.length > 0"
            :src="scope.row.cover[0].url | fixImgUrl"
          />
          <img class="thumbnail" v-else src="../assets/logo.png" alt="" />
        </template>
      </el-table-column>

      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="warning"
            @click="$router.push('/editpost?id=' + scope.row.id)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageIndex: 1,
      pageSizes: 10,
      postList: [],
      total: 0,
    };
  },

  created() {
    this.loadPage();
  },

  methods: {
    currentChange(newpageIndex) {
      this.pageIndex = newpageIndex;
      this.loadPage();
    },

    sizeChange(newpageSizes) {
      this.pageSizes = newpageSizes;
      this.loadPage();
    },

    loadPage() {
      this.$axios({
        url: "/post",
        params: { pageIndex: this.pageIndex, pageSizes: this.pageSizes },
      }).then((res) => {
        console.log(res.data);
        this.postList = res.data.data;
        this.total = res.data.total;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.thumbnail {
  width: 100%;
  height: 120px;
  object-fit: cover;
}
</style>